/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React, {useState} from 'react';
import {SafeAreaView, ScrollView, Text, Button} from 'react-native';
import {
  Picker,
  SelectCameraPosition,
  SelectContactFilterType,
  SelectContactFilterCondition,
  ScanType,
  PhotoViewMIMETypes,
  CompleteButtonText,
  DocumentSelectMode,
} from '@hadss/react_native_picker';

function App(): JSX.Element {
  const [result, setResult] = useState('');

  const onPressAudio = async (): Promise<void> => {
    const res = await Picker.selectAudio({});
    setResult(JSON.stringify(res));
  };

  const onPressSelectCamera = async (): Promise<void> => {
    const res = await Picker.selectCamera(['photo', 'video'], {
      cameraPosition: SelectCameraPosition.CAMERA_POSITION_BACK,
    });
    setResult(JSON.stringify(res));
  };

  const onPressContact = async (): Promise<void> => {
    const res = await Picker.selectContact({
      filter: {
        filterClause: {
          name: [
            {
              filterCondition: SelectContactFilterCondition.NOT_IN,
              value: ['a', 'b'],
            },
          ],
        },
        filterType: SelectContactFilterType.SHOW_FILTER,
      },
    });
    setResult(JSON.stringify(res));
  };

  const onPressDocument = async (): Promise<void> => {
    const res = await Picker.selectDocument({
      maxSelectNumber: 3,
      selectMode: DocumentSelectMode.FILE,
    });
    setResult(JSON.stringify(res));
  };

  const onPressQueryLocation = async (): Promise<void> => {
    let queryLocationOptions = {
      location: {
        latitude: 39.9175,
        longitude: 116.3972,
      },
      name: '故宫博物院',
    };
    await Picker.queryLocation(queryLocationOptions);
  };

  const onPressChooseLocation = async (): Promise<void> => {
    const res = await Picker.chooseLocation({});
    setResult(JSON.stringify(res));
  };

  const onPressSelectDistrict = async (): Promise<void> => {
    const res = await Picker.selectDistrict({});
    setResult(JSON.stringify(res));
  };

  const onPressPhoto = async (): Promise<void> => {
    const res = await Picker.selectPhoto({
      MIMEType: PhotoViewMIMETypes.IMAGE_TYPE,
      completeButtonText: CompleteButtonText.TEXT_ADD,
    });
    setResult(JSON.stringify(res));
  };

  const onPressStartScan = async (): Promise<void> => {
    const res = await Picker.startScan({
      scanTypes: [ScanType.QR_CODE],
      enableAlbum: false,
    });
    setResult(JSON.stringify(res));
  };

  return (
    <SafeAreaView>
      <ScrollView contentInsetAdjustmentBehavior="automatic">
        <Text>Picker:</Text>
        <Button title="selectAudio" onPress={onPressAudio} />
        <Button title="selectCamera" onPress={onPressSelectCamera} />
        <Button title="selectContact" onPress={onPressContact} />
        <Button title="selectDocument" onPress={onPressDocument} />
        <Button title="queryLocation" onPress={onPressQueryLocation} />
        <Button title="chooseLocation" onPress={onPressChooseLocation} />
        <Button title="selectDistrict" onPress={onPressSelectDistrict} />
        <Button title="selectPhoto" onPress={onPressPhoto} />
        <Button title="startScan" onPress={onPressStartScan} />
        <Text>结果: {result}</Text>
      </ScrollView>
    </SafeAreaView>
  );
}

export default App;
